package com.example.anliku.controller;

import com.example.anliku.Utils.RedisUtil;
import com.example.anliku.entity.Example;
import com.example.anliku.entity.ExampleForES;
import com.example.anliku.entity.Message;
import com.example.anliku.mapper.*;
import com.example.anliku.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "统计模块")
@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {
    @Resource
    private StatisticsService statisticsService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private DownloadService downloadService;

    @Resource
    private CommentService commentService;

    @ApiOperation(value = "获取所有统计信息")
    @GetMapping("/all")
    public Message selectAll() {
        return statisticsService.StatisticsAll();
    }

    @ApiOperation(value = "根据案例id获取下载量")
    @GetMapping("/caseId")
    public Message DownloadByCaseId(Integer exampleId) {
        return statisticsService.DownloadByCaseId(exampleId);
    }

    @ApiOperation(value = "获取全年下载量")
    @GetMapping("/clazzAndKno")
    public Message clazzAndKno(String year) {
        return statisticsService.clazzAndKno(year);
    }

    @ApiOperation(value = "获取发布案例数量最多的三个老师和案例数")
    @GetMapping("/selectBig")
    public Message selectBig() {
        return statisticsService.selectBig();
    }

    @ApiOperation(value = "下载量和评论量同步redis")
    @PostMapping("/post")
    public Message post() {
        List<Map> mapList = commentService.returnSum();
        for (Map map : mapList) {
            redisUtil.set("comment"+map.get("commentCaseId"),map.get("commentSum"));
        }
        List<Map> mapList1 = downloadService.returnSum();
        for (Map map : mapList1) {
            redisUtil.set("download"+map.get("downloadCaseId"),map.get("downloadSum"));
        }
        return Message.success("数据同步成功");
    }
}
